package mi.payment.ui.admin;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.BuildConfig;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.settings.TransactionSwitchSetting;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.title.Title;

/**
 * 传统类交易开关控制
 * Created by lj on 2017/2/14.
 */
public class TraditionalTransactionActivity extends ParentActivity {
    private View mTraditionalTransactionConsumption, mTraditionalTransactionPurchaseCancellation, mTraditionalTransactionSalesReturn, mTraditionalTransactionQueryBalance, mTraditionalTransactionPreAuthorization, mTraditionalTransactionPreAuthorizationToCancel, mTraditionalTransactionPreAuthorizationToCompleteTheRequest, mTraditionalTransactionCompletedPreAuthorization, mTraditionalTransactionCompletedPreAuthorizationRevoked;
    private ButtonSwitch mTraditionalTransactionConsumptionSwitch, mTraditionalTransactionPurchaseCancellationSwitch, mTraditionalTransactionSalesReturnSwitch, mTraditionalTransactionQueryBalanceSwtich, mTraditionalTransactionPreAuthorizationSwitch, mTraditionalTransactionPreAuthorizationToCancelSwitch, mTraditionalTransactionPreAuthorizationToCompleteTheRequestSwitch, mTraditionalTransactionCompletedPreAuthorizationSwitch, mTraditionalTransactionCompletedPreAuthorizationRevokedSwitch;
    private TransactionSwitchOperation mTransactionSwitchOperation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_traditional_transaction);
        mTransactionSwitchOperation = TransactionSwitchOperation.getInstance();
        initView();
        setClickListener();
        setCheckChangeListener();
        updateUI();
    }

    private void initView() {
        Title mTitle;
        TextView mLeftText;
        mTitle = (Title) findViewById(R.id.traditional_transaction_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.traditional_transaction_switch_settings));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TraditionalTransactionActivity.this.finish();
            }
        }, null, null, null);

        mTraditionalTransactionConsumption = findViewById(R.id.traditional_transaction_consumption);
        mTraditionalTransactionPurchaseCancellation = findViewById(R.id.traditional_transaction_purchase_cancellation);
        mTraditionalTransactionSalesReturn = findViewById(R.id.traditional_transaction_sales_return);
        mTraditionalTransactionQueryBalance = findViewById(R.id.traditional_transaction_query_balance);
        mTraditionalTransactionPreAuthorization = findViewById(R.id.traditional_transaction_pre_authorization);
        mTraditionalTransactionPreAuthorizationToCancel = findViewById(R.id.traditional_transaction_pre_authorization_to_cancel);
        mTraditionalTransactionPreAuthorizationToCompleteTheRequest = findViewById(R.id.traditional_transaction_pre_authorization_to_complete_the_request);
        mTraditionalTransactionCompletedPreAuthorization = findViewById(R.id.traditional_transaction_completed_pre_authorization);
        mTraditionalTransactionCompletedPreAuthorizationRevoked = findViewById(R.id.traditional_transaction_completed_pre_authorization_revoked);

        mLeftText = (TextView) mTraditionalTransactionConsumption.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.consumption));

        mLeftText = (TextView) mTraditionalTransactionPurchaseCancellation.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.cancellation));

        mLeftText = (TextView) mTraditionalTransactionSalesReturn.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.return_goods));

        mLeftText = (TextView) mTraditionalTransactionQueryBalance.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.balance_query));

        mLeftText = (TextView) mTraditionalTransactionPreAuthorization.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.traditional_transaction_pre_auth));

        mLeftText = (TextView) mTraditionalTransactionPreAuthorizationToCancel.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.traditional_transaction_pre_auth_cancel));

        mLeftText = (TextView) mTraditionalTransactionPreAuthorizationToCompleteTheRequest.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.traditional_transaction_pre_authorization_complete_request));



        mLeftText = (TextView) mTraditionalTransactionCompletedPreAuthorization.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.traditional_transaction_pre_authorization_complete));

        if(Configs.getInstance().HIDE_PRE_AUTHORIZATION_NOTICE()) {//隐藏预授权完成（通知）
            mTraditionalTransactionCompletedPreAuthorization.setVisibility(View.GONE);
        }

        mLeftText = (TextView) mTraditionalTransactionCompletedPreAuthorizationRevoked.findViewById(R.id.switch_item_text);
        mLeftText.setText(getResources().getString(R.string.traditional_transaction_pre_auth_complete_cancel));

        mTraditionalTransactionConsumptionSwitch = (ButtonSwitch) mTraditionalTransactionConsumption.findViewById(R.id.set_switch);
        mTraditionalTransactionPurchaseCancellationSwitch = (ButtonSwitch) mTraditionalTransactionPurchaseCancellation.findViewById(R.id.set_switch);
        mTraditionalTransactionSalesReturnSwitch = (ButtonSwitch) mTraditionalTransactionSalesReturn.findViewById(R.id.set_switch);
        mTraditionalTransactionQueryBalanceSwtich = (ButtonSwitch) mTraditionalTransactionQueryBalance.findViewById(R.id.set_switch);
        mTraditionalTransactionPreAuthorizationSwitch = (ButtonSwitch) mTraditionalTransactionPreAuthorization.findViewById(R.id.set_switch);
        mTraditionalTransactionPreAuthorizationToCancelSwitch = (ButtonSwitch) mTraditionalTransactionPreAuthorizationToCancel.findViewById(R.id.set_switch);
        mTraditionalTransactionPreAuthorizationToCompleteTheRequestSwitch = (ButtonSwitch) mTraditionalTransactionPreAuthorizationToCompleteTheRequest.findViewById(R.id.set_switch);
        mTraditionalTransactionCompletedPreAuthorizationSwitch = (ButtonSwitch) mTraditionalTransactionCompletedPreAuthorization.findViewById(R.id.set_switch);
        mTraditionalTransactionCompletedPreAuthorizationRevokedSwitch = (ButtonSwitch) mTraditionalTransactionCompletedPreAuthorizationRevoked.findViewById(R.id.set_switch);

    }

    private void setCheckChangeListener() {
        mTraditionalTransactionConsumptionSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveConsumption(isChecked);
            }
        });

        mTraditionalTransactionPurchaseCancellationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveConsumptionRevoke(isChecked);
            }
        });

        mTraditionalTransactionSalesReturnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveReturnGoods(isChecked);
            }
        });

        mTraditionalTransactionQueryBalanceSwtich.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveQueryBalance(isChecked);
            }
        });

        mTraditionalTransactionPreAuthorizationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveFieldAuth(isChecked);
            }
        });

        mTraditionalTransactionPreAuthorizationToCancelSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveFieldAuthRevoke(isChecked);
            }
        });

        mTraditionalTransactionPreAuthorizationToCompleteTheRequestSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveFieldAuthReq(isChecked);
            }
        });

        mTraditionalTransactionCompletedPreAuthorizationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveFieldAuthCompleteRevocationNotice(isChecked);
            }
        });

        mTraditionalTransactionCompletedPreAuthorizationRevokedSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mTransactionSwitchOperation.saveFieldAuthCompleteRevocation(isChecked);
            }
        });
        updateUI();
    }

    /**
     * 设置点击事件监听
     */
    private void setClickListener() {
        mTraditionalTransactionConsumption.setOnClickListener(this);
        mTraditionalTransactionPurchaseCancellation.setOnClickListener(this);
        mTraditionalTransactionSalesReturn.setOnClickListener(this);
        mTraditionalTransactionQueryBalance.setOnClickListener(this);
        mTraditionalTransactionPreAuthorization.setOnClickListener(this);
        mTraditionalTransactionPreAuthorizationToCancel.setOnClickListener(this);
        mTraditionalTransactionPreAuthorizationToCompleteTheRequest.setOnClickListener(this);
        mTraditionalTransactionCompletedPreAuthorization.setOnClickListener(this);
        mTraditionalTransactionCompletedPreAuthorizationRevoked.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);


        switch (v.getId()) {
            case R.id.traditional_transaction_consumption:
                //消费
                mTraditionalTransactionConsumptionSwitch.setOnCheck(!mTraditionalTransactionConsumptionSwitch.isChecked());
                break;
            case R.id.traditional_transaction_purchase_cancellation:
                //消费撤销
                mTraditionalTransactionPurchaseCancellationSwitch.setOnCheck(!mTraditionalTransactionPurchaseCancellationSwitch.isChecked());

                break;
            case R.id.traditional_transaction_sales_return:
                //退货
                mTraditionalTransactionSalesReturnSwitch.setOnCheck(!mTraditionalTransactionSalesReturnSwitch.isChecked());

                break;
            case R.id.traditional_transaction_query_balance:
                //查询余额
                mTraditionalTransactionQueryBalanceSwtich.setOnCheck(!mTraditionalTransactionQueryBalanceSwtich.isChecked());

                break;
            case R.id.traditional_transaction_pre_authorization:
                //预授权
                mTraditionalTransactionPreAuthorizationSwitch.setOnCheck(!mTraditionalTransactionPreAuthorizationSwitch.isChecked());

                break;
            case R.id.traditional_transaction_pre_authorization_to_cancel:
                //预授权撤销
                mTraditionalTransactionPreAuthorizationToCancelSwitch.setOnCheck(!mTraditionalTransactionPreAuthorizationToCancelSwitch.isChecked());

                break;
            case R.id.traditional_transaction_pre_authorization_to_complete_the_request:
                //预授权完成请求
                mTraditionalTransactionPreAuthorizationToCompleteTheRequestSwitch.setOnCheck(!mTraditionalTransactionPreAuthorizationToCompleteTheRequestSwitch.isChecked());

                break;
            case R.id.traditional_transaction_completed_pre_authorization:
                //预授权完成（通知）
                mTraditionalTransactionCompletedPreAuthorizationSwitch.setOnCheck(!mTraditionalTransactionCompletedPreAuthorizationSwitch.isChecked());

                break;
            case R.id.traditional_transaction_completed_pre_authorization_revoked:
                //预授权完成撤销
                mTraditionalTransactionCompletedPreAuthorizationRevokedSwitch.setOnCheck(!mTraditionalTransactionCompletedPreAuthorizationRevokedSwitch.isChecked());
                break;
        }
    }

    /**
     * 更新UI
     */
    private void updateUI() {
        TransactionSwitchSetting mTransactionSwitchSetting = mTransactionSwitchOperation.getTransactionSwitchSetting();
        mTraditionalTransactionConsumptionSwitch.setOnCheck(mTransactionSwitchSetting.isConsumption());

        mTraditionalTransactionPurchaseCancellationSwitch.setOnCheck(mTransactionSwitchSetting.isConsumptionRevoke());

        mTraditionalTransactionSalesReturnSwitch.setOnCheck(mTransactionSwitchSetting.isReturnGoods());

        mTraditionalTransactionQueryBalanceSwtich.setOnCheck(mTransactionSwitchSetting.isQueryBalance());

        mTraditionalTransactionPreAuthorizationSwitch.setOnCheck(mTransactionSwitchSetting.isFieldAuth());

        mTraditionalTransactionPreAuthorizationToCancelSwitch.setOnCheck(mTransactionSwitchSetting.isFieldAuthRevoke());

        mTraditionalTransactionPreAuthorizationToCompleteTheRequestSwitch.setOnCheck(mTransactionSwitchSetting.isFieldAuthReq());

        mTraditionalTransactionCompletedPreAuthorizationSwitch.setOnCheck(mTransactionSwitchSetting.isFieldAuthCompleteRevocationNotice());

        mTraditionalTransactionCompletedPreAuthorizationRevokedSwitch.setOnCheck(mTransactionSwitchSetting.isFieldAuthCompleteRevocation());
    }
}
